package com.example.voucher.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.voucher.contract.VoucherItemCapable;
import com.example.voucher.contract.spi.VoucherItemCapableSalesInvoiceAdapter;
import com.example.voucher.entity.Company;
import com.example.voucher.entity.SalesInvoice;
import com.example.voucher.entity.SalesInvoiceSn;
import com.example.voucher.extension.ExtensionImpl;
import com.example.voucher.mapper.CompanyMapper;
import com.example.voucher.mapper.SalesInvoiceMapper;
import com.example.voucher.mapper.SalesInvoiceSnMapper;
import com.example.voucher.service.SalesInvoiceQueryService;
import com.google.common.collect.Lists;

@ExtensionImpl(extendFrom = SalesInvoiceQueryService.class)
public class DefaultSalesInvoiceQueryServiceImpl implements SalesInvoiceQueryService {

    @Autowired
    private SalesInvoiceMapper invoiceMapper;
    @Autowired
    private SalesInvoiceSnMapper invoiceSnMapper;
    @Autowired
    private CompanyMapper companyMapper;    

    @Override
    public List<VoucherItemCapable> queryVoucherItem(List<Long> pkidList) {
        // 查发票主表
        // LambdaQueryWrapper<Invoice> queryWrapper = Wrappers.<Invoice>lambdaQuery();
        // queryWrapper.eq(Invoice::getValid, CommonConstant.VALID_T).in(Invoice::getPkid, pkidList);
        // List<Invoice> invoiceList = invoiceMapper.selectList(queryWrapper);
        List<SalesInvoice> invoiceList = invoiceMapper.selectBatchIds(pkidList);

        // 查发票明细表
        LambdaQueryWrapper<SalesInvoiceSn> querySnWrapper = Wrappers.<SalesInvoiceSn>lambdaQuery();
        // querySnWrapper.eq(InvoiceSn::getValid, CommonConstant.VALID_T).in(InvoiceSn::getAwaitPriintId, pkidList);
        querySnWrapper.in(SalesInvoiceSn::getAwaitPriintId, pkidList);
        List<SalesInvoiceSn> invoiceSnList = invoiceSnMapper.selectList(querySnWrapper);

        // 查公司表
        Set<Long> orgTitleIdSet = invoiceList.stream().map(SalesInvoice::getOrgTitleId).collect(Collectors.toSet());
        List<Company> companyList = companyMapper.selectBatchIds(orgTitleIdSet);
        Map<Long, Company> companyMap = companyList.stream().collect(Collectors.toMap(Company::getPkid, Function.identity()));
        
        // 组装VoucherItemCapable
        List<VoucherItemCapable> list = Lists.newArrayList();
        Map<Long, SalesInvoice> invoiceMap =
            invoiceList.stream().collect(Collectors.toMap(SalesInvoice::getPkid, Function.identity()));
        for (SalesInvoiceSn invoiceSn : invoiceSnList) {
            SalesInvoice invoice = invoiceMap.get(invoiceSn.getAwaitPriintId());
            Company company = companyMap.get(invoice.getOrgTitleId());
            list.add(new VoucherItemCapableSalesInvoiceAdapter(invoice, invoiceSn, company));
        }

        return list;
    }

}
